<?php


namespace core\utils;


use app\common\service\ConfigService;
use Exception;
use OSS\OssClient;

class OssUtil
{
    static function upload($path,$content)
    {
        $config = ConfigService::getConfig("FILE_UPLOAD_CONFIG");
        if (!$config) {
            throw new Exception("阿里云OSS配置不存在:");
        }
        $accessKeyId = $config['accessKeyId'];
        $accessKeySecret = $config['accessKeySecret'];
        $endpoint = $config['endpoint'];
        $domain = $config['domain'] ?? "";

        $oss = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        try {
            $oss->putObject($config['bucket'], ltrim($path, '/'),$content);
            return $domain . $path;
        } catch (Exception $exception) {
            throw new Exception("OSS上传失败:" . $exception->getMessage());
        }
    }
}